package com.service.admin.admin.service.impl;

import com.google.common.collect.Lists;
import com.service.admin.admin.controller.request.MenuReq;
import com.service.admin.admin.controller.response.MenuResp;
import com.service.admin.admin.controller.response.UserMenuResp;
import com.service.admin.admin.dto.LoginUserSession;
import com.service.admin.admin.service.SysMenuService;
import com.service.dao.po.MenuItemPO;
import com.service.dao.mapper.SysMenuMapper;
import com.service.dao.mapper.SysRoleMenuMapper;
import com.service.dao.mapper.SysUserRoleMapper;
import com.service.admin.support.SessionUtil;
import com.service.dao.model.SysMenu;
import com.service.dao.model.SysUserRole;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年07月27日</dd>
 * </dl>
 *
 * @author
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysRoleMenuMapper menuRoleMapper;

    @Autowired
    private SessionUtil sessionUtil;

    @Override
    public MenuResp getMenuById(Integer id) {
        SysMenu sysMenu = sysMenuMapper.selectById(id);
        return ModelMapperUtil.getStrictModelMapper().map(sysMenu, MenuResp.class);
    }

    @Override
    public List<MenuItemPO> listAllMenu() {
        return sysMenuMapper.listAll();
    }

    @Override
    public List<MenuItemPO> listAllMenuTree() {
        List<MenuItemPO> menuItemPOList = sysMenuMapper.listAll();
        Integer bottomLevel = sysMenuMapper.getBottomLevel();
        int topLevel = 1;
        List<MenuItemPO> resultMenuList = Lists.newLinkedList();
        if (!ObjectUtils.isEmpty(menuItemPOList)) {
            for (int i = bottomLevel; i >= topLevel; i--) {
                int level = i;
                List<MenuItemPO> newMenuList = Lists.newLinkedList();
                List<MenuItemPO> menuListTemp = menuItemPOList.stream().filter(dto -> dto.getLevel().equals(level)).collect(Collectors.toList());
                if (!ObjectUtils.isEmpty(menuListTemp)) {
                    newMenuList.addAll(menuListTemp);
                }
                for (MenuItemPO item : newMenuList) {
                    List<MenuItemPO> childrenList = resultMenuList.stream().filter(vo -> ObjectUtils.nullSafeEquals(item.getId(), vo.getParentId())).collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(childrenList))
                        item.setChildren(childrenList);
                }
                resultMenuList.clear();
                resultMenuList = newMenuList;
            }
        }
        return resultMenuList;
    }

    @Override
    public List<UserMenuResp> initUserMenu() {

        LoginUserSession session = sessionUtil.getUser();
        List<SysUserRole> userRoleList = userRoleMapper.listUserRoleByRoleIdAndRoleStatus(session.getId(), true);
        List<Integer> roleIdList = Lists.newArrayList();
        userRoleList.forEach(item -> roleIdList.add(item.getSysRoleId()));

        List<MenuItemPO> roleMenuList = sysMenuMapper.listMenuByRoleList(roleIdList);
        List<UserMenuResp> resultList = Lists.newArrayList();
        roleMenuList.forEach(menuItem ->{
            resultList.add(ModelMapperUtil.getStrictModelMapper().map(menuItem, UserMenuResp.class));
        });
        return resultList;
    }

    @Override
    @Transactional
    public void saveMenu(MenuReq menuReq) {
        SysMenu sysMenu = ModelMapperUtil.getStrictModelMapper().map(menuReq, SysMenu.class);
        int level = 2;
        if (!ObjectUtils.isEmpty(menuReq.getParentId())) {
            SysMenu menuItem = sysMenuMapper.selectById(menuReq.getParentId());
            level = menuItem.getLevel() + 1;
        } else {
            sysMenu.setParentId(1);
        }
        sysMenu.setLevel(level);
        if (ObjectUtils.isEmpty(menuReq.getId())) {
            sysMenuMapper.insert(sysMenu);
        } else {
            sysMenuMapper.updateById(sysMenu);
        }
    }

    @Override
    @Transactional
    public void deleteMenu(Integer id) {
       SysMenu menu = sysMenuMapper.selectById(id);
       if(!ObjectUtils.isEmpty(menu)){
           menu = new SysMenu();
           menu.setId(id);
           menu.setEnabled(false);
           sysMenuMapper.updateById(menu);
       }

    }
}
